﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.Basic
{
    public partial class EGUserInControlerChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<EGUser> users = SDDbSession.Default.From<EGUser>().ToList();
                BindDept();
                BindUser(users);
            }
        }
        #endregion
        protected void BindDept()
        {
            List<Department> lists = SDDbSession.Default.From<Department>().ToList();
            cbl_dept.DataSource = lists;
            cbl_dept.DataTextField = "DeptName";
            cbl_dept.DataValueField = "DeptID";
            cbl_dept.DataBind();
        }
        protected void BindUser(List<EGUser> users)
        {
            string str = "";
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                List<EGUserInControler> lists = SDDbSession.Default.From<EGUserInControler>().Where(EGUserInControler._.EGControlerID == id).ToList();
                if (lists != null)
                {
                    foreach (var list in lists)
                    {
                        str += "[" + list.EGUserID + "]";
                    }
                }
            }
            foreach (EGUser row in users)
            {
                CheckItem ci = new CheckItem();
                ci.Text = row.Name;
                ci.Value = row.EGUserID.ToString();
                if (str != "" && str.Contains("[" + row.EGUserID + "]"))
                {
                    ci.Selected = true;
                }
                cbl_user.Items.Add(ci);
            }
            
        }

        #region 保存数据
        
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            EGUser Tm = new EGUser();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                string id = Request["id"];
                SDDbSession.Default.FromSql("delete from EGUserInControler where EGControlerid=" + id).ExecuteNonQuery();
                foreach (string s in cbl_user.SelectedValueArray)
                {
                    returnvalue += SDDbSession.Default.FromSql("insert into EGUserInControler(EGControlerid,EGUserID) values(" + id + "," + s + ") ").ExecuteNonQuery();
                }
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        #endregion

        protected void cbl_dept_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbl_user.Items.Clear();
            if (cbl_dept.SelectedValueArray.Length > 0)
            {
                List<EGUser> users = SDDbSession.Default.From<EGUser>().Where(EGUser._.DeptID.SelectIn(cbl_dept.SelectedValueArray)).ToList();
                BindUser(users);
            }
            else
            {
                List<EGUser> users = SDDbSession.Default.From<EGUser>().ToList();
                BindUser(users);
            }
        }
    }
}